package com.weixing.mall.provider.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.annotation.SysLog;
import com.weixing.mall.core.enums.OperateEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.AgentAuth;
import com.weixing.mall.core.support.auth.AuthUtil;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.provider.model.domain.Agent;
import com.weixing.mall.provider.model.dto.AgentRefundDto;
import com.weixing.mall.provider.model.query.AgentRefundQuery;
import com.weixing.mall.provider.model.vo.AgentRefundVo;
import com.weixing.mall.provider.service.IAgentRefundService;
import com.weixing.mall.provider.service.IAgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 退费记录列表Api
 *
 * @author  gjq
 * @date  2019/9/24
 * @since  1.0
 **/
@Validated
@RestController
@RequestMapping("agent/refund")
public class AgentRefundController extends BaseController {

    @Autowired
    private IAgentRefundService agentRefundService;
    @Autowired
    private IAgentService agentService;


    /**
     * 分页加载代理商退费列表
     *
     * @param agentRefundQuery 查询条件
     * @return 返回结果
     */
    @GetMapping("")
    public Result<IPage<AgentRefundVo>> list(AgentRefundQuery agentRefundQuery){
        SysAuth auth = AuthUtil.getSysExt();
        IPage<AgentRefundVo> pageInfo = agentRefundService.selectOfPage(initMpPage(),agentRefundQuery,auth);
        return ResultUtil.success(pageInfo);
    }

    /**
     * 分页加载代理商退费记录列表
     *
     * @param agentId 代理id
     * @return 记录
     */
    @GetMapping("list/{agentId}")
    public Result<Map<String,Object>> refundPage(@PathVariable Long agentId){
        Agent agent = agentService.getById(agentId);
        Map<String,Object> result = new HashMap<>(8);
        result.put("agentId",agent.getId());
        result.put("agentSn",agent.getAgentSn());
        result.put("agentLevel",agent.getAgentLevel());
        result.put("mobile",agent.getMobile());
        result.put("customerName",agent.getCustomerName());
        return ResultUtil.success(result);
    }


    /**
     * 分页加载代理商退费列表---代理用
     *
     * @param query 查询条件
     * @return 结果
     */
    @GetMapping("/agent")
    public Result<IPage<AgentRefundVo>> listForAgent(AgentRefundQuery query){
        AgentAuth auth = AuthUtil.getAgentExt();
        query.setAgentId(auth.getAid());
        IPage<AgentRefundVo> pageInfo = agentRefundService.selectOfPage(initMpPage(),query,null);
        return ResultUtil.success(pageInfo);
    }


    /**
     * 保存代理商退费信息
     *
     * @param agentRefundDto 参数
     * @return 结果
     */
    @PostMapping("")
    @NoRepeatSubmit
    @SysLog(type = OperateEnum.S, desp = "代理退费")
    public Result save(AgentRefundDto agentRefundDto){
        SysAuth auth = AuthUtil.getSysExt();
        agentRefundService.save(agentRefundDto,auth);
        return ResultUtil.success();
    }

}
